/*
 * SeePhoto.java
 *
 * Created on 15 f?vrier 2008, 13:57
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package br.great.functionalCore;

import br.great.beans.*;
import java.io.*;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
//import org.xml.sax.SAXException;
import org.xml.sax.SAXException;
//import org.apache.xerces.parsers.DOMParser;
/**
 *
 * @author defautce
 */
public class SeePhoto {

    private static String dossier;

    /** Creates a new instance of SeePhoto */
    public SeePhoto() {
    }

    private static String getDossier(String uri) {
        int i = uri.lastIndexOf("\\");
        if (i >= 0) {
            return uri.substring(0, i + 1);
        } else {
            return null;
        }
    }

    public static void openPhoto(Photo photo, int typeCollection) {
       if (typeCollection==0){ //Mobile Collection
          FileInputStream in = null;
            try {
                String uriOntology = photo.getURIAnnotations();
                dossier = getDossier(uriOntology);
                in = new FileInputStream(uriOntology);
                Model model = ModelFactory.createMemModelMaker().createModel();
                model.read(in, null);
                // requ?te SPARQL pour r?cup?rer nexif:fileSource => romanForum1.jpg
                String querySourcePhoto = "PREFIX nexif:<http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#>  SELECT ?source WHERE {?photo nexif:fileSource ?source }";
                Query query = QueryFactory.create(querySourcePhoto);
                QueryExecution qe = QueryExecutionFactory.create(query, model);
                ResultSet results = qe.execSelect();
                String xml = ResultSetFormatter.asXMLString(results);
    //            System.out.println(xml);
                DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document document = parser.parse(new InputSource(new StringReader(xml)));
                Element root = (Element) document.getDocumentElement();
                String sourcePhoto = getSource(root);
                photo.setURIPhoto(dossier + sourcePhoto);
            } catch (SAXException ex) {
                Logger.getLogger(SeePhoto.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(SeePhoto.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SeePhoto.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(SeePhoto.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        else{ //FlickrCollection
            photo.setURIPhoto("/images/Photomap.png");
        }
    }

    private static String getSource(Node node) {
        String nodeName = node.getNodeName();
        if (nodeName.equals("binding")) {
            //System.out.println("value binding : "+node.getNodeValue());
            NamedNodeMap nnm = node.getAttributes();
            Node n = nnm.getNamedItem("name");
            //System.out.println("value name : "+n.getNodeValue());
            if ((n != null) && (n.getNodeValue().equals("source"))) {
                if (node.hasChildNodes()) {
                    NodeList nodeListAnnot = node.getChildNodes();
                    int size = nodeListAnnot.getLength();
                    for (int i = 0; i < size; i++) {
                        Node l = nodeListAnnot.item(i);
                        if (l.getNodeName().equals("literal")) {
                            Node c = l.getFirstChild();
                            return c.getNodeValue();
                        }
                    }
                }
            }
        }
        if (node.hasChildNodes()) {
            NodeList nodeList = node.getChildNodes();
            int size = nodeList.getLength();
            for (int i = 0; i < size; i++) {
                String res = getSource(nodeList.item(i));
                if (res != null) {
                    return res;
                }
            }
        }

        return null;
    }
}
